﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Newtonsoft.Json.Linq;
using SuperMicroWave.Common;
using SuperMicroWave.LogFile;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SuperMicroWave.ViewModels
{
    public partial class StatusViewModel : ObservableObject
    {
        public StatusViewModel() 
        {
            Thread thread = new Thread(new ThreadStart(UpdateState));
            thread.IsBackground = true;
            thread.Start();
        }


        #region 数据源
        [ObservableProperty]
        public bool pump1_Checked;

        [ObservableProperty]
        public bool pump2_Checked;

        [ObservableProperty]
        public bool pump3_Checked;

        [ObservableProperty]
        public bool pump4_Checked;

        [ObservableProperty]
        public bool pump5_Checked;

        [ObservableProperty]
        public bool pump6_Checked;

        [ObservableProperty]
        public bool pump7_Checked;

        [ObservableProperty]
        public bool pump8_Checked;

        [ObservableProperty]
        public bool buzzer_Checked;

        [ObservableProperty]
        public bool lock_Checked;

        #endregion


        #region 命令函数
        [RelayCommand]
        public void Pump1(object o)
        {
            string str = (bool)o ? "打开" : "关闭";
            LogHelper.WriteInfoLog("进气阀：" + str + "（手动）");
            GlobalValue.modBusTcp.WriteCoil("0", (bool)o);
        }

        [RelayCommand]
        public void Pump2(object o)
        {
            string str = (bool)o ? "打开" : "关闭";
            LogHelper.WriteInfoLog("排气阀：" + str + "（手动）");
            GlobalValue.modBusTcp.WriteCoil("1", (bool)o);
        }

        [RelayCommand]
        public void Pump3(object o)
        {
            string str = (bool)o ? "打开" : "关闭";
            LogHelper.WriteInfoLog("冷水阀：" + str + "（手动）");
            GlobalValue.modBusTcp.WriteCoil("2", (bool)o);
        }

        [RelayCommand]
        public void Pump4(object o)
        {
            string str = (bool)o ? "打开" : "关闭";
            LogHelper.WriteInfoLog("冷水机：" + str + "（手动）");
            GlobalValue.modBusTcp.WriteCoil("3", (bool)o);
        }

        [RelayCommand]
        public void Pump5(object o)
        {
            GlobalValue.modBusTcp.WriteCoil("4", (bool)o);
        }

        [RelayCommand]
        public void CanCtrBtn(object o)
        {
            GlobalValue.modBusTcp.WriteRegister("1", Convert.ToUInt16(o));
        }

        [RelayCommand]
        public void Buzzer(object o)
        {
            GlobalValue.modBusTcp.WriteCoil("5", (bool)o);
        }

        [RelayCommand]
        public void LockBtn(object o)
        {
            GlobalValue.modBusTcp.WriteCoil("6", (bool)o);
        }
        #endregion


        #region 功能函数
        public void UpdateState()
        {
            while (true)
            {
                bool[]? value = GlobalValue.modBusTcp.ReadCoils("0", 7);
                if (value != null)
                {
                    Pump1_Checked = value[0];
                    Pump2_Checked = value[1];
                    Pump3_Checked = value[2];
                    Pump4_Checked = value[3];
                    Pump5_Checked = value[4];
                    Buzzer_Checked = value[5];
                    Lock_Checked = value[6];
                }

                bool[]? value1 = GlobalValue.modBusTcp.ReadDis("2", 3);
                if (value1 != null)
                {
                    Pump6_Checked = !value1[0];
                    Pump7_Checked = !value1[1];
                    Pump8_Checked = !value1[2];
                }
                Thread.Sleep(1000);
            }
        }
        #endregion


        /*
        #region
        partial void OnPump1_CheckedChanged(bool value)
        {
            string str = value ? "打开" : "关闭";
            LogHelper.WriteInfoLog("进气阀：" + str);
        }
        partial void OnPump2_CheckedChanged(bool value)
        {
            string str = value ? "打开" : "关闭";
            LogHelper.WriteInfoLog("排气阀：" + str);
        }
        partial void OnPump3_CheckedChanged(bool value)
        {
            string str = value ? "打开" : "关闭";
            LogHelper.WriteInfoLog("冷水阀：" + str);
        }
        partial void OnPump4_CheckedChanged(bool value)
        {
            string str = value ? "打开" : "关闭";
            LogHelper.WriteInfoLog("冷水机：" + str);
        }
        #endregion
        */
    }
}
